package com.sunducation.wts.lifecycle

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View

//lifecycle是一个生命周期感知组件 它可以感知Fragment/Activity的生命周期 并且会防止内存泄漏
class MainActivity : AppCompatActivity() {
    //liecycle 用于解耦，用于处理需要注册和取消的问题
    private lateinit var lifeCycleObserver : MyActivityLifeCycleObserver

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        lifeCycleObserver = MyActivityLifeCycleObserver()
        lifecycle.addObserver(lifeCycleObserver)
    }

    override fun onDestroy() {
        super.onDestroy()
        lifecycle.removeObserver(lifeCycleObserver)
    }

    fun testStartService(view: View) {
        startService(Intent(this, MyLifeCycleService::class.java))
    }
    fun testStopService(view: View) {
        stopService(Intent(this, MyLifeCycleService::class.java))
    }

    fun testMy2Activity(view: View) {
        startActivity(Intent(this, My2Activity::class.java))
    }
}
